﻿using nplJobsCommon.JobSeeker;
using nplJobsDAL.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace nplJobsDAL.Select
{
    public class SelectUserTypeByEmail : DataAccessBase
    {
        public string EMail { get; set; }

        public SelectUserTypeByEmail()
        {
            StoredProcedureName = StoredProcedure.Name.spSelectUserTypeByEmail.ToString();
        }

        #region GetUserTypeByEmail
        public UserType GetUserTypeByEmail()
        {
            DataBaseHelper dbHelper;
            DataSet dsUserType;

            try
            {
                SelectJobSeekerByEmailParameters selectJobSeekerParameters = new SelectJobSeekerByEmailParameters(this.EMail);
                dbHelper = new DataBaseHelper(StoredProcedureName);
                dbHelper.Parameters = selectJobSeekerParameters.Parameters;
                dsUserType = dbHelper.Run(base.ConnectionString, dbHelper.Parameters);
                return GetList(dsUserType);
            }
            catch (Exception)
            {
                throw;
            }
        }
        #endregion



        #region SQL parameters for selecting job seeker language proficiency by jobseeker id
        public class SelectJobSeekerByEmailParameters
        {
            private SqlParameter[] _parameters;

            public SelectJobSeekerByEmailParameters(string eMail)
            {
                this.EMail = eMail;
                Build();
            }
            private void Build()
            {
                SqlParameter[] parameters = 
                {
                    new SqlParameter("@EMail", EMail),
                };

                Parameters = parameters;
            }

            public string EMail { get; set; }

            public SqlParameter[] Parameters
            {
                get { return _parameters; }
                set { _parameters = value; }
            }
        }
        #endregion
        #region GetList
        private UserType GetList(DataSet dsUserType)
        {
            UserType userType;
            try
            {
                userType = (from n in dsUserType.Tables[0].AsEnumerable()
                                                select new UserType
                                                {
                                                    User = n.Field<string>("UserType"),
                                                    ID = n.Field<Guid>("ID")
                                                }).FirstOrDefault();

                return userType;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
            finally { }
        }
        #endregion

    }
}
